<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('vote.vote.vote_type_id')" type="remoteSelect" v-model="baTable.form.items!.vote_type_id" prop="vote_type_id" :input-attr="{ pk: 'ba_vote_type.id', field: 'name', remoteUrl: '/admin/vote.Type/index' }" :placeholder="t('Please select field', { field: t('vote.vote.vote_type_id') })" />
                    <FormItem :label="t('vote.vote.title')" type="string" v-model="baTable.form.items!.title" prop="title" :placeholder="t('Please input field', { field: t('vote.vote.title') })" />
                    <FormItem :label="t('vote.vote.titles')" type="string" v-model="baTable.form.items!.titles" prop="titles" :placeholder="t('Please input field', { field: t('vote.vote.titles') })" />
                    <FormItem :label="t('vote.vote.titless')" type="string" v-model="baTable.form.items!.titless" prop="titless" :placeholder="t('Please input field', { field: t('vote.vote.titless') })" />
                    <FormItem :label="t('vote.vote.images')" type="images" v-model="baTable.form.items!.images" prop="images" />
                    <FormItem :label="t('vote.vote.editor')" type="editor" v-model="baTable.form.items!.editor" prop="editor" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)" :placeholder="t('Please input field', { field: t('vote.vote.editor') })" />
                    <FormItem :label="t('vote.vote.lng')" type="string" v-model="baTable.form.items!.lng" prop="lng" :placeholder="t('Please input field', { field: t('vote.vote.lng') })" />
                    <FormItem :label="t('vote.vote.lat')" type="string" v-model="baTable.form.items!.lat" prop="lat" :placeholder="t('Please input field', { field: t('vote.vote.lat') })" />
                    <FormItem :label="t('vote.vote.poiname')" @click="openMapPickPop()" type="string" v-model="baTable.form.items!.poiname" prop="poiname" :placeholder="t('Please input field', { field: t('vote.vote.poiname') })" />
                    <FormItem :label="t('vote.vote.switch')" type="switch" v-model="baTable.form.items!.switch" prop="switch" :input-attr="{ content: { '0': t('vote.vote.switch 0'), '1': t('vote.vote.switch 1') } }" />
<!--                    <FormItem :label="t('vote.vote.num')" type="string" v-model="baTable.form.items!.num" prop="num" :placeholder="t('Please input field', { field: t('vote.vote.num') })" />-->
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    editor: [buildValidatorData({ name: 'editorRequired', title: t('vote.vote.editor') })],
    create_time: [buildValidatorData({ name: 'date', title: t('vote.vote.create_time') })],
})
import { openTencentMapPickPop } from '/@/components/xmmap/tencentMap'
import { getTencentmapKey } from '/@/api/backend/xmmap/tencentmap'
import { ElNotification } from 'element-plus'
const state = reactive({
    tencentMapKey: 'O5RBZ-OFMRU-ZV5VI-B6N3H-Q77FJ-5GF3A',
})
// 弹窗打开地图方法
const openMapPickPop = () => {
    if (state.tencentMapKey == '') {
        ElNotification({
            type: 'error',
            message: '请前往【系统配置】【第三方密钥】配置腾讯地图KEY',
        })
        return false
    }
    openTencentMapPickPop(state.tencentMapKey)
}
window.addEventListener('message', function (event) {
    if (event.data.type == 'tencent-map-data') {
        const currentData = JSON.parse(event.data.data)
        console.log('腾讯地图当前位置数据：', currentData)
        baTable.form.items!.poiname = currentData.poiname
        baTable.form.items!.lng = currentData.latlng.lng
        baTable.form.items!.lat = currentData.latlng.lat
    }
})
</script>

<style scoped lang="scss"></style>
